﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace System
{
    public class RegexExtension
    {
        /// <summary>
        /// 判断字符串是否是a-zA-Z0-9_范围内（3-50位范围内）
        /// </summary>
        /// <param name="strIn"></param>
        /// <returns></returns>
        public static bool IsValidUserName(string strIn)
        {
            return Regex.IsMatch(strIn, @"^[A-Za-z0-9_]{3,50}$");
        }
        /// <summary>
        /// 验证邮箱
        /// </summary>
        /// <param name="strIn"></param>
        /// <returns></returns>
        public static bool IsValidEmail(string strIn)
        {
            return Regex.IsMatch(strIn, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
        }
        /// <summary>
        /// 验证手机
        /// </summary>
        /// <param name="strIn"></param>
        /// <returns></returns>
        public static bool IsValidMobile(string strIn)
        {
            if (string.IsNullOrEmpty(strIn))
            {
                return false;
            }
            else
            {
                return Regex.IsMatch(strIn, @"^[1]+[3,4,5,7,8,9]+\d{9}$");
            }

        }
        /// <summary>
        /// 验证手机号
        /// </summary>
        /// <param name="strIn"></param>
        /// <returns></returns>
        public static bool IsValidTelPhone(string strIn)
        {
            return Regex.IsMatch(strIn, @"^(\d{3,4}-)?\d{6,8}$");


        }

        public static string GetExtend(string value)
        {
            Regex regex = new Regex("[^/]+(?!.*[7z|zip|rar])");
            var match = regex.Match(value).Groups;
            if (match.Count > 0)
                return match[0].Value;
            else
                return "";
        }
        public static string GetNum(string value)
        {
            Regex regex = new Regex(@"[\d]+");
            var match = regex.Match(value).Groups;
            if (match.Count > 0)
                return match[0].Value;
            else
                return "";
        }
        public static string GetNumWithOutZero(string value)
        {

            Regex regex = new Regex(@"^[1-9]*");
            var match = regex.Match(value).Groups;
            if (match.Count > 0)
                return match[0].Value;
            else
                return "";
        }
        public static string GetPicExtend(string value)
        {
            Regex regex = new Regex("[^/]+(?!.*[png|jpeg|jpg])");
            var match = regex.Match(value).Groups;
            if (match.Count > 0)
                return match[0].Value;
            else
                return DateTime.Now.ToString();
        }
        public static string Regexlookbehind(string value)
        {
            string pattern = @"(?<=[SmartIoT])(.*?)(?=[D])";
            Regex regex = new Regex(pattern);

            var match = regex.Match(value).Groups;
            if (match.Count > 0)
                return match[0].Value;
            else
                return null;
        }
        public static string RegexlookbehindAndFront(string value, string front, string behind)
        {
            string pattern = $"{front}.*?{behind}.*";
            Regex regex = new Regex(pattern);

            var match = regex.Match(value).Groups;
            if (match.Count > 0)
                return match[0].Value;
            else
                return match[1].Value;
        }
        public static string RegexImg(string img, string regexImg)
        {
            try
            {
                if (string.IsNullOrEmpty(img)) return "";
                string res = "src=\"";
                Regex re = new Regex(res); 
                return img;
            }
            catch (Exception)
            {
                return img;
            }
        }
    }
}
